package com.fuqi.android.zhongcai.base.view.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.android.volley.Response;
import com.android.volley.error.VolleyError;
import com.fuqi.android.zhongcai.R;
import com.fuqi.android.zhongcai.base.view.widget.CountDownButton;
import com.fuqi.android.zhongcai.base.request.api.MsgVerifyCodeApi;
import com.fuqi.android.zhongcai.base.request.api.VerifyCodeApi;
import com.fuqi.android.zhongcai.base.request.result.MsgVerifyCodeResult;
import com.fuqi.android.zhongcai.base.request.result.VerifyCodeResult;
import com.fuqi.android.zhongcai.utils.ActivityUtils;
import com.fuqi.framework.network.code.HandleCode;
import com.fuqi.framework.network.http.RequestManager;

import butterknife.Bind;
import butterknife.OnClick;


public class ForgetPassFirstActivity extends BaseActivity implements CountDownButton.CountDownDelegateListener {

    @Bind(R.id.btn_getcode)
    CountDownButton btnGetcode;

    @Bind(R.id.et_forgetpass_phone)
    EditText et_forgetpass_phone;
    @Bind(R.id.et_forgetpass_vercode)
    EditText et_forgetpass_vercode;
    @Bind(R.id.forgetpassnext)
    Button forgetpassnext;

    private String et_forgetpass_phonestr,et_forgetpass_vercodstr ;

    @OnClick({R.id.btn_getcode,R.id.forgetpassnext })
    public void onBtnClick(View view) {
        et_forgetpass_phonestr     =  et_forgetpass_phone.getText().toString().trim();
        switch (view.getId()){
            case R.id.btn_getcode:
                if(TextUtils.isEmpty(et_forgetpass_phonestr)){
                    ActivityUtils.toast("请输入手机号");
                    break;
                }else{
                    RequestManager.getInstance().call(new MsgVerifyCodeApi(new MsgVerifyCodeApi.MsgVerifyCodeParams(et_forgetpass_phonestr), resultListener, errorListener));
                }
                break;
            case R.id.forgetpassnext:

                et_forgetpass_vercodstr   =  et_forgetpass_vercode.getText().toString().trim();

                if(TextUtils.isEmpty(et_forgetpass_phonestr)){
                    //此处可以加一个手机号码的验证
                    ActivityUtils.toast("请输入手机号");
                    break;
                }else if(TextUtils.isEmpty(et_forgetpass_vercodstr)){
                    ActivityUtils.toast("请输入验证码");
                    break;
                }else{

                    RequestManager.getInstance().call(new VerifyCodeApi(new VerifyCodeApi.VerifyCodeParams(
                            et_forgetpass_phonestr,et_forgetpass_vercodstr), respVerifyListener, errorListener));


                }

                break;
        }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        titleBar.setTitle("修改密码");
    }


    Response.Listener<MsgVerifyCodeResult> resultListener = new Response.Listener<MsgVerifyCodeResult>() {

        @Override
        public void onResponse(MsgVerifyCodeResult response) {


            btnGetcode.countDown();
            ActivityUtils.toast("验证码发送成功，请注意查收");

        }
    };

    Response.Listener<VerifyCodeResult> respVerifyListener = new Response.Listener<VerifyCodeResult>() {

        @Override
        public void onResponse(VerifyCodeResult response) {


            if (HandleCode.requestSuccess()) {

                Bundle bundle = new Bundle();
                bundle.putString("phone",et_forgetpass_phonestr);
                bundle.putString("vercode",et_forgetpass_vercodstr);
                ActivityUtils.startActivity(ForgetPassFirstActivity.this,ForgetPassTwoActivity.class,bundle);
                finish();

            } else {

                ActivityUtils.toast("验证失败");

            }
        }
    };


    Response.ErrorListener errorListener = new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("juner", "juner  error.getMessage(): " + error.getMessage());
        }
    };


    @Override
    public void onCountDown(int seconds) {
        btnGetcode.setText("" + seconds);
    }

    @Override
    public void onCountDownonFinished() {
        btnGetcode.setText("重新获取");
    }


    @Override
    public void initView() {

    }

    @Override
    public void initListener() {

    }

    @Override
    public void initData() {

    }

    @Override
    public boolean hasTitleBar() {
        return true;
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_forgetpassfirst;
    }
}
